package by.grsu.verkhovodko.page;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.verkhovodko.app.VerkhovodkoWebApplication;
import by.grsu.verkhovodko.entity.Passenger;
import by.grsu.verkhovodko.dao.impl.PassengerDao;

public class Registration extends WebPage{


	/**
	 * 
	 */
	private static final long serialVersionUID = -5029087924565358519L;

	@SuppressWarnings({ "rawtypes",  "serial" })
	public Registration() {
		super();
		final Passenger passenger= new Passenger();
		final Form<Passenger> form = new Form<Passenger>("form",new CompoundPropertyModel<Passenger>(passenger));
		add(form);
		
		form.add(new TextField<String>("login"));
		form.add(new TextField<String>("password"));
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				final PassengerDao passengerDao = new PassengerDao(
						VerkhovodkoWebApplication.DATA_XML_FOLDER);
				if (passenger.getId() == null) {
					passengerDao.saveNew(passenger);
				} else {
					passengerDao.update(passenger);
				}
				setResponsePage(StartPage.class); 
			}
		});

		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(StartPage.class);
			}
		});
	}

}
